/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.pay.service;

import java.util.List;
import java.util.Map;

import com.jeesite.modules.pay.entity.Tpay;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.pay.entity.Tpayment;
import com.jeesite.modules.pay.dao.TpaymentDao;
import com.jeesite.modules.pay.entity.TpaymentDetail;
import com.jeesite.modules.pay.dao.TpaymentDetailDao;

/**
 * 付款单Service
 * @author wsm
 * @version 2021-03-16
 */
@Service
@Transactional(readOnly=true)
public class TpaymentService extends CrudService<TpaymentDao, Tpayment> {
	
	@Autowired
	private TpaymentDetailDao tpaymentDetailDao;
	@Autowired
	private TpaymentDao tpaymentDao;
	
	/**
	 * 获取单条数据
	 * @param tpayment
	 * @return
	 */
	@Override
	public Tpayment get(Tpayment tpayment) {
		Tpayment entity = super.get(tpayment);
		if (entity != null){
			TpaymentDetail tpaymentDetail = new TpaymentDetail(entity);
			tpaymentDetail.setStatus(TpaymentDetail.STATUS_NORMAL);
			entity.setTpaymentDetailList(tpaymentDetailDao.findList(tpaymentDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param tpayment 查询条件
	 * @param tpayment.page 分页对象
	 * @return
	 */
	@Override
	public Page<Tpayment> findPage(Tpayment tpayment) {
		return super.findPage(tpayment);
	}


	/**
	 * 保存数据（插入或更新）
	 * @param tpayment
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Tpayment tpayment) {
		super.save(tpayment);
		// 保存 Tpayment子表
		for (TpaymentDetail tpaymentDetail : tpayment.getTpaymentDetailList()){
			if (!TpaymentDetail.STATUS_DELETE.equals(tpaymentDetail.getStatus())){
				tpaymentDetail.setPaymentId(tpayment);
				if (tpaymentDetail.getIsNewRecord()){
					tpaymentDetailDao.insert(tpaymentDetail);
				}else{
					tpaymentDetailDao.update(tpaymentDetail);
				}
			}else{
				tpaymentDetailDao.delete(tpaymentDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param tpayment
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Tpayment tpayment) {
		super.updateStatus(tpayment);
	}
	
	/**
	 * 删除数据
	 * @param tpayment
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Tpayment tpayment) {
		super.delete(tpayment);
		TpaymentDetail tpaymentDetail = new TpaymentDetail();
		tpaymentDetail.setPaymentId(tpayment);
		tpaymentDetailDao.deleteByEntity(tpaymentDetail);
	}

	/**
	 * 付款选择页面-代付款列表
	 * @param tpayment
	 * @return
	 */
	@Transactional(readOnly=false)
	public List<Tpay> getPaidInfo(Tpayment tpayment){
		List<Tpay> paidInfo = tpaymentDao.getPaidInfo(tpayment);
		return paidInfo;
	}
	
}